<?php

use App\Enums\AssetSourceType;
use App\Enums\AssetStatus;
use App\Models\Asset;
use App\Models\FrontendUser;
use App\Services\WalletService;

require __DIR__ . '/../vendor/autoload.php';

$app = require_once __DIR__ . '/../bootstrap/app.php';

$app->make(\App\Console\Kernel::class)->bootstrap();

/* ============== Write Codes Below ================= */

echo '>>> Start Laravel from CLI' . PHP_EOL;

DB::transaction(function () {
    /** @var WalletService $walletService */
    $walletService = app()->get(WalletService::class);

    $agentId = 4;
    $contractAddress = '0x90150407c3ea3deabddfb7270a5a23a346861da5';
    $fromMobile = '15238071442';

    $toMobiles = '18511085379';

    $number = 5;
    $fromUser = FrontendUser::query()
        ->where('mobile', $fromMobile)
        ->where('agent_id', $agentId)
        ->first();

    if (!$fromUser) {
        dd("资产拥有人{$fromMobile}未找到");
    }

    $toUser = $walletService->openWallet($toMobiles);

    if (empty($toUser)) {
        dd('转账接收人没有找到');
    }

    $assets = Asset::query()
        ->where('user_id', $fromUser->id)
        ->where('contract_address', $contractAddress)
        ->where('asset_status', AssetStatus::HOLD)
        ->where('source_type', AssetSourceType::AIRDROP())
        ->limit($number)
        ->get();

    if (count($assets) < $number) {
        dd('持有资产数量不足');
    }

    foreach ($assets as $index => $asset) {
        $fromAsset = $asset;

        try {
            $transResult = $walletService->transfer(
                $fromMobile,
                $contractAddress,
                [$fromAsset->token_id],
                $toUser
            );

            $transHash = Arr::get($transResult, $fromAsset->token_id);
            if (!$transHash) {
                throw new \Symfony\Component\HttpKernel\Exception\HttpException('转账返回值错误');
            }

            $product_token = \App\Models\ProductToken::where('token_id', $fromAsset->token_id)->first();

            $product_token->status = \App\Enums\TokenStatus::INIT();
            $product_token->lock_type = null;
            $product_token->lock_refer_id = null;

            $product_token->save();

//            $toAsset = new Asset();
//            $toAsset->user_id = $toUser->id;
//            $toAsset->source_type = AssetSourceType::GIVE;
//            $toAsset->source_id = $fromAsset->id;
//            $toAsset->contract_address = $fromAsset->contract_address;
//            $toAsset->token_id = $fromAsset->token_id;
//            $toAsset->trans_hash = $transHash;
//            $toAsset->asset_status = AssetStatus::HOLD;
//            $toAsset->save();

            \App\Models\Airdrop::where('id', $fromAsset->source_id)->delete();

            $fromAsset->delete();
        } catch (\Exception $e) {
            dump($e->getMessage());
            $transResult = $walletService->transfer(
                $toUser->mobile,
                $contractAddress,
                [$fromAsset->token_id],
                $fromUser->wallet_address
            );
            dump('资产归还给原账户');

            throw $e;
        }
    }
});
